"""
RagFlow 客户端 GUI 九宫格主页组件
"""

from PySide6.QtCore import Qt, Signal
from PySide6.QtWidgets import (
    QWidget, QVBoxLayout, QGridLayout, QPushButton, QLabel
)

from ragflow_client.gui.features import (
    ConfigFeature, UploadFeature, BatchUploadFeature, BatchParseFeature, LogFeature,
    DatasetsFeature, DocumentsFeature, SearchFeature, ChatFeature, HomeFeature
)


class HomeGridWidget(QWidget):
    """九宫格主页组件"""
    
    # 定义信号
    show_tab_signal = Signal(int)
    show_datasets_signal = Signal()
    show_documents_signal = Signal()
    show_search_signal = Signal()
    show_chat_signal = Signal()
    show_home_signal = Signal()
    
    def __init__(self, parent=None):
        """初始化九宫格主页组件"""
        super().__init__(parent)
        self.parent = parent
        self.setup_ui()
        self.setup_features()
    
    def setup_features(self):
        """设置功能模块"""
        self.features = {
            "config": ConfigFeature(self.parent),
            "upload": UploadFeature(self.parent),
            "batch_upload": BatchUploadFeature(self.parent),
            "batch_parse": BatchParseFeature(self.parent),
            "log": LogFeature(self.parent),
            "datasets": DatasetsFeature(self.parent),
            "documents": DocumentsFeature(self.parent),
            "search": SearchFeature(self.parent),
            "chat": ChatFeature(self.parent),
            "home": HomeFeature(self.parent)
        }
    
    def setup_ui(self):
        """创建界面"""
        # 创建主布局
        main_layout = QVBoxLayout(self)
        
        # 创建九宫格布局
        grid_layout = QGridLayout()
        main_layout.addLayout(grid_layout)
        
        # 定义功能按钮
        functions = [
            {"name": "配置", "icon": "⚙️", "feature": "config", "action": lambda: self.show_tab_signal.emit(0)},
            {"name": "上传文件", "icon": "📤", "feature": "upload", "action": lambda: self.show_tab_signal.emit(1)},
            {"name": "批量上传", "icon": "📦", "feature": "batch_upload", "action": lambda: self.show_tab_signal.emit(2)},
            {"name": "批量解析", "icon": "🔄", "feature": "batch_parse", "action": lambda: self.execute_feature("batch_parse")},
            {"name": "查看日志", "icon": "📝", "feature": "log", "action": lambda: self.show_tab_signal.emit(3)},
            {"name": "知识库列表", "icon": "📚", "feature": "datasets", "action": lambda: self.show_datasets_signal.emit()},
            {"name": "文档列表", "icon": "📄", "feature": "documents", "action": lambda: self.show_documents_signal.emit()},
            {"name": "搜索", "icon": "🔍", "feature": "search", "action": lambda: self.show_search_signal.emit()},
            {"name": "对话", "icon": "💬", "feature": "chat", "action": lambda: self.show_chat_signal.emit()},
            {"name": "返回主页", "icon": "🏠", "feature": "home", "action": lambda: self.show_home_signal.emit()}
        ]
        
        # 创建功能按钮
        row, col = 0, 0
        for func in functions:
            # 创建按钮
            button = QPushButton()
            
            # 创建垂直布局来放置图标和文本
            button_layout = QVBoxLayout(button)
            button_layout.setContentsMargins(10, 10, 10, 10)
            button_layout.setSpacing(5)
            
            # 创建图标标签
            icon_label = QLabel(func["icon"])
            icon_label.setAlignment(Qt.AlignCenter)
            icon_label.setStyleSheet("font-size: 32px;")
            button_layout.addWidget(icon_label)
            
            # 创建文本标签
            text_label = QLabel(func["name"])
            text_label.setAlignment(Qt.AlignCenter)
            text_label.setStyleSheet("font-size: 16px; font-weight: bold;")
            button_layout.addWidget(text_label)
            
            # 按钮样式将由主应用程序的主题设置
            
            button.setMinimumSize(120, 120)
            button.setMaximumSize(200, 200)
            
            # 连接按钮点击事件到功能执行方法
            feature_name = func["feature"]
            button.clicked.connect(lambda checked, name=feature_name: self.execute_feature(name))
            
            # 添加到网格
            grid_layout.addWidget(button, row, col)
            
            # 更新行列
            col += 1
            if col > 2:  # 每行3个按钮
                col = 0
                row += 1
    
    def execute_feature(self, feature_name):
        """
        执行指定的功能
        
        Args:
            feature_name: 功能名称
        """
        # 发送对应的信号
        if feature_name == "config":
            self.show_tab_signal.emit(0)
        elif feature_name == "upload":
            self.show_tab_signal.emit(1)
        elif feature_name == "batch_upload":
            self.show_tab_signal.emit(2)
        elif feature_name == "batch_parse":
            # 执行批量解析功能
            if feature_name in self.features:
                self.features[feature_name].execute()
        elif feature_name == "log":
            self.show_tab_signal.emit(3)
        elif feature_name == "datasets":
            self.show_datasets_signal.emit()
        elif feature_name == "documents":
            self.show_documents_signal.emit()
        elif feature_name == "search":
            self.show_search_signal.emit()
        elif feature_name == "chat":
            self.show_chat_signal.emit()
        elif feature_name == "home":
            self.show_home_signal.emit()
